package br.com.drestranho.escola.managed;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import br.com.drestranho.escola.dao.MatriculaDAO;
import br.com.drestranho.escola.entity.Matricula;

@ManagedBean  
@SessionScoped
public class RelatorioManager {
	@EJB
	MatriculaDAO matriculaDAO = new MatriculaDAO();
	
	private StreamedContent file;  
    
    public StreamedContent getFile() {  
    	System.out.println("file apanhado."+file.getName());
        return file;  
    }    

	public void relMatricula() {
		try {
			System.out.println("Iniciando...");
			JasperReport report=null;
			try {
				report = JasperCompileManager.compileReport(new URI("http://localhost:8080/EscolaWeb/faces/pages/templates/Report2em_bco.jrxml").toURL().openStream());
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
			Map<String,Object> parameters = new HashMap<String,Object>();
			parameters.put("Title", "Relatório de Matrículas");
			List<Matricula> lista = matriculaDAO.getBeans();
			JRBeanCollectionDataSource bcds = new JRBeanCollectionDataSource(lista);  
			
			JasperPrint print = JasperFillManager.fillReport(report, parameters, bcds);
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			InputStream byteArrayInputStream = null;
			
			JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, byteArrayOutputStream);
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
			exporter.exportReport();
			
			byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
			
			System.out.println("tamanho: "+byteArrayOutputStream.toByteArray().length);
			file = new DefaultStreamedContent(byteArrayInputStream, "application/pdf", "alunos.pdf");
			System.out.println(file.getContentType());
		} catch (JRException e) {
			System.out.println("Deu pau!");
			e.printStackTrace();
		}
		System.out.println("Relatório gerado.");
	}
	
}
